package com.demo2do.fms.admin.web.controller.supplier;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.SupplierBusinessHistoryService;
import com.demo2do.fms.admin.service.supplier.SupplierCertificateService;
import com.demo2do.fms.admin.service.supplier.SupplierInfoService;
import com.demo2do.fms.admin.service.supplier.SupplierParkingResourceService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.service.supplier.SupplierStaffService;
import com.demo2do.fms.admin.service.supplier.TransSupplierService;
import com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService;
import com.demo2do.fms.admin.service.supplier.WareSupplierEmployeeService;
import com.demo2do.fms.admin.service.supplier.WareSupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.supplier.common.KeyPost;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.SupplierBusinessHistory;
import com.demo2do.fms.entity.supplier.common.SupplierCertificate;
import com.demo2do.fms.entity.supplier.common.SupplierEvent;
import com.demo2do.fms.entity.supplier.common.SupplierEventType;
import com.demo2do.fms.entity.supplier.common.SupplierInfo;
import com.demo2do.fms.entity.supplier.common.SupplierParkingResource;
import com.demo2do.fms.entity.supplier.common.SupplierRecommendation;
import com.demo2do.fms.entity.supplier.common.SupplierStaff;
import com.demo2do.fms.entity.supplier.trans.TrailerType;
import com.demo2do.fms.entity.supplier.trans.TransSupplier;
import com.demo2do.fms.entity.supplier.trans.TransSupplierVehicleStat;
import com.demo2do.fms.entity.supplier.trans.VehicleType;
import com.demo2do.fms.entity.supplier.ware.WareSupplier;
import com.demo2do.fms.entity.supplier.ware.WareSupplierEmployee;

/**
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("suppliers")
@MenuSetting("menu-supplier")
public class SupplierProfileEditController {
    
    private static final Logger logger = LoggerFactory.getLogger(SupplierProfileEditController.class);
    
    @Autowired
    private SupplierService supplierService;
    
    @Autowired
    private SupplierInfoService supplierInfoService;
    
    @Autowired
    private SupplierCertificateService supplierCertificateService;
    
    @Autowired
    private SupplierBusinessHistoryService supplierBusinessHistoryService;
    
    @Autowired
    private SupplierParkingResourceService supplierParkingResourceService;
    
    @Autowired
    private SupplierStaffService  supplierStaffService;
    
    @Autowired
    private TransSupplierVehicleStatService transSupplierVehicleStatService;
    
    @Autowired
    private TransSupplierService transSupplierService;
    
    @Autowired
    private WareSupplierService wareSupplierService;
    
    @Autowired
    private WareSupplierEmployeeService wareSupplierEmployeeService;
    
    /**
     * view  公司信息管理页面详情
     *
     * @param supplierId
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}", method = RequestMethod.GET)
    @MenuSetting("submenu-{supplierMenuType}-supplier-management")
    public ModelAndView view(@PathVariable("supplierId") Long supplierId,
                             @RequestParam("supplierMenuType") String supplierMenuType) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("supplierMenuType", supplierMenuType);
        
        //运输和仓储公共的字段 
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        SupplierCertificate supplierCertificate = supplierCertificateService.getSupplierCertificate(supplierId);
        SupplierInfo supplierInfo = supplierInfoService.load(supplierId);
        List<SupplierBusinessHistory> supplierBusinessHistorys = supplierBusinessHistoryService.listSupplierBusinessHistory(supplierId);
        List<SupplierParkingResource> parkingResources = supplierParkingResourceService.listSupplierParkingResource(supplierId);
        List<SupplierStaff> supplierStaffs = null;
        // 一般存储供方只查询 分管副总 和 项目经理/大客户经理
        if (supplier.getType().isCommonStorage()){
            supplierStaffs = supplierStaffService.listCommonStorageSupplierStaff(supplierId);
        }else{
            supplierStaffs = supplierStaffService.listSupplierStaff(supplierId);
        }
        SupplierRecommendation recommendation = supplierService.getSupplierRecommendation(supplierId);
        
        modelAndView.addObject("supplier", supplier);
        modelAndView.addObject("supplierCertificate", supplierCertificate);
        modelAndView.addObject("supplierInfo", supplierInfo);
        modelAndView.addObject("supplierBusinessHistorys", supplierBusinessHistorys);
        modelAndView.addObject("parkingResources", parkingResources);
        modelAndView.addObject("supplierStaffs", supplierStaffs);
        modelAndView.addObject("recommendation", recommendation);
       
        //运输供方
        if (supplier.getType().getAlias().equalsIgnoreCase("motor")) {

            modelAndView.setViewName("supplier/profile/supplier-profile-motor-view");

            List<TransSupplierVehicleStat> transSupplierVehicleStats = transSupplierVehicleStatService.listTransSupplierVehicleStat(supplierId);
            TransSupplier transSupplier = transSupplierService.getTransSupplier(supplierId);
            SupplierEvent supplierEvent = supplierService.getSupplierEvent(supplierId, SupplierEventType.INTRODUCE_REJECTED);
            SupplierEvent changeSupplierEvent = supplierService.getSupplierEvent(supplierId, SupplierEventType.CHANGE_REJECTED);
            
            modelAndView.addObject("transSupplierVehicleStats", transSupplierVehicleStats);
            modelAndView.addObject("transSupplier", transSupplier);
            modelAndView.addObject("supplierEvent", supplierEvent);
            modelAndView.addObject("changeSupplierEvent", changeSupplierEvent);

        } else if (supplier.getType().isStorage()){

            modelAndView.setViewName("supplier/profile/supplier-profile-storage-view");

            WareSupplier wareSupplier = wareSupplierService.getWareSupplier(supplierId);
            List<WareSupplierEmployee> wareSupplierEmployees = wareSupplierEmployeeService.listWareSupplierEmployee(supplierId);

            modelAndView.addObject("wareSupplier", wareSupplier);
            modelAndView.addObject("wareSupplierEmployees", wareSupplierEmployees);

        } else if (supplier.getType().isCommonStorage()){

            modelAndView.setViewName("supplier/profile/supplier-profile-common-storage-view");

        }
       
        return modelAndView;
        
    }
    
    /**
     * 修改公司业务信息panel
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-business", method = RequestMethod.GET)
    public ModelAndView supplierBusinessEdit(@PathVariable("supplierId") Long supplierId) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-business-edit");
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        
        modelAndView.addObject("supplier", supplier);
        
        return modelAndView;
        
    }
    
    /**
     * 修改公司业务信息
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-business", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSupplierBusinessEdit(@Preloading("supplierId") Supplier supplier,
                                                           @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            supplierService.update(supplier.operate(operator));
            
            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onSupplierBusinessEdit() - edit fail, exception = [{}]", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
        
    }
    
    /**
     * 修改公司基本信息panel
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-info", method = RequestMethod.GET ,params = "panel")
    public ModelAndView supplierInfoEdit(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-info-edit");
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        SupplierInfo supplierInfo = supplierInfoService.load(supplierId);
        
        modelAndView.addObject("supplier", supplier);
        modelAndView.addObject("supplierInfo", supplierInfo);
        
        return modelAndView;
    }

    /**
     * 修改公司基本信息
     * 
     * @param supplier
     * @param supplierInfo
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-info", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSupplierInfoEdit(@Preloading("supplierId") Supplier supplier, 
                                                       @Preloading("supplierInfoId") SupplierInfo supplierInfo,
                                                       @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        try {
            
            supplierInfoService.edit(supplier , supplierInfo, operator);

            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onSupplierInfoEdit() - edit fail, exception = [{}]", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
        
    }
    
    /**
     * 上传公司基本信息文件
     * 
     * @param supplierId
     * @param type
     * 
     *        - businessLicense: 营业执照原件扫描件上传 
     *        - qualitySystem: 质量体系认证证书原件扫描件上传
     *        - leaseContract: 办公及停车场地租房/购房合同扫描件上传
     *        - transportCert: 道路许可证上传
     *        
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-info/upload", method = RequestMethod.GET, params = "type")
    public ModelAndView supplierInfoUpload(@PathVariable("supplierId") Long supplierId,
                                           @RequestParam("type") String type,
                                           @RequestParam("supplierMenuType") String supplierMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-info-upload");
        
        modelAndView.addObject("supplierId", supplierId);
        
        SupplierInfo supplierInfo = supplierInfoService.load(supplierId);
        modelAndView.addObject("supplierInfo", supplierInfo);
        
        modelAndView.addObject("type", type);
        modelAndView.addObject("supplierMenuType", supplierMenuType);
        
        return modelAndView;
        
    }
    
    /**
     * 修改基本业务信息panel
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-certificate", method = RequestMethod.GET, params = "panel")
    public ModelAndView SupplierCertificateEdit(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-certificate-edit");
        
        
        SupplierCertificate supplierCertificate = supplierCertificateService.getSupplierCertificate(supplierId);
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        
        modelAndView.addObject("supplierCertificate", supplierCertificate);
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("supplier", supplier);

        return modelAndView;
    }

    /**
     * 修改基本业务信息
     * 
     * @param supplierCertificate
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-certificate", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSupplierCertificateEdit(@Preloading("supplierCertificateId") SupplierCertificate supplierCertificate,
                                                              @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        try {

            supplierCertificateService.edit(supplierCertificate, operator);

            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onSupplierCertificateEdit() - edit failed, exception={}", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
        
    }
    
    /**
     * 上传基本业务信息文件
     * 
     * @param supplierId
     * @param type
     *        - briefCert: 公司简介上传
     *        - propertyCert: 房产证扫描证上传
     *        - auditCert: 申请方近三年的审计报告上传
     *        - taxCert： 申请方近三年的所得税表上传
     *        - financialCert： 银行资信等级证明上传
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-certificate/upload", method = RequestMethod.GET, params = "type")
    public ModelAndView supplierCertificateUpload(@PathVariable("supplierId")Long supplierId,
                                                  @RequestParam("type")String type,
                                                  @RequestParam("supplierMenuType") String supplierMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-certificate-upload");
        
        modelAndView.addObject("supplierId", supplierId);
        
        SupplierCertificate supplierCertificate = supplierCertificateService.getSupplierCertificate(supplierId);
        modelAndView.addObject("supplierCertificate", supplierCertificate);
        
        modelAndView.addObject("type", type);
        modelAndView.addObject("supplierMenuType", supplierMenuType);
        
        return modelAndView;
    }
    
    /**
     * 创建公司过往业务信息panel
     * 
     * @param supplierId
     * @param type
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history/create", method = RequestMethod.GET)
    public ModelAndView businessHistoryCreate(@PathVariable("supplierId") Long supplierId,
                                              @RequestParam("type") String type) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/business-history-create");
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        modelAndView.addObject("supplier", supplier);
        
        modelAndView.addObject("type", type);

        return modelAndView;
        
    }

    /**
     * 创建公司过往业务信息
     * 
     * @param supplierBusinessHistory
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onBusinessHistoryCreate(SupplierBusinessHistory supplierBusinessHistory,
                                                            @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        supplierBusinessHistoryService.save(supplierBusinessHistory.initialize(operator));
        
        httpResult.success().message("创建成功");

        return httpResult;
        
    }

    /**
     * 修改公司过往业务信息panel
     * 
     * @param businessHistoryId
     * @param supplierId
     * @param type
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history/{businessHistoryId}/edit", method = RequestMethod.GET)
    public ModelAndView businessHistoryEdit(@PathVariable("businessHistoryId") Long businessHistoryId, 
                                            @PathVariable("supplierId") Long supplierId,
                                            @RequestParam("type") String type) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/business-history-edit");
        
        SupplierBusinessHistory supplierBusinessHistory =  supplierBusinessHistoryService.load(SupplierBusinessHistory.class, businessHistoryId);
        modelAndView.addObject("supplierBusinessHistory", supplierBusinessHistory);
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        modelAndView.addObject("supplier", supplier);
        
        modelAndView.addObject("type", type);

        return modelAndView;
        
    }

    /**
     * on edit 公司过往业务信息
     *  
     * @param login
     * @param supplierBusinessHistory
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history/{businessHistoryId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onBusinessInfoEdit(@Secure Login login, 
                                                       @Preloading("id") SupplierBusinessHistory supplierBusinessHistory) {

        HttpResult httpResult = new HttpResult();
        
        supplierBusinessHistoryService.edit(supplierBusinessHistory.edit(login.getId()));

        httpResult.success().message("修改成功");

        return httpResult;
        
    }

    /**
     * delete 公司过往业务信息
     * 
     * @param businessHistoryId
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history/{businessHistoryId}/delete", method = RequestMethod.GET)
    public ModelAndView businessInfoDelete(@PathVariable("businessHistoryId") Long businessHistoryId , @PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/business-history-delete");
        
        modelAndView.addObject("businessHistoryId", businessHistoryId);
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
    }

    /**
     * on delete 公司过往业务信息
     * 
     * @param businessHistoryId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history/{businessHistoryId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @MenuSetting("submenu-company-info-management")
    public @ResponseBody HttpResult onBusinessInfoDelete(@PathVariable("businessHistoryId") Long businessHistoryId) {

        HttpResult httpResult = new HttpResult();
        
        SupplierBusinessHistory supplierBusinessInfo = supplierBusinessHistoryService.load(SupplierBusinessHistory.class, businessHistoryId);
        
        supplierBusinessHistoryService.delete(supplierBusinessInfo.delete());

        httpResult.success().message("删除成功");

        return httpResult;
        
    }
    
    /**
     * 过往业务信息-上传合同
     * 
     * @param businessHistoryId
     * @param supplierId
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/business-history/{businessHistoryId}/contract-upload", method = RequestMethod.GET)
    public ModelAndView businessContactUpload(@PathVariable("businessHistoryId") Long businessHistoryId ,
                                              @PathVariable("supplierId") Long supplierId,
                                              @RequestParam("supplierMenuType") String supplierMenuType) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/business-history-contract-upload");
        
        modelAndView.addObject("businessHistoryId", businessHistoryId);
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("supplierMenuType", supplierMenuType);

        return modelAndView;
        
    }
    
    /**
     * create 停车场资源信息
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/parking-resource/create", method = RequestMethod.GET)
    public ModelAndView parkingCreate(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/parking-resource-create");
        
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
    }

    /**
     * on create 停车场资源信息
     * 
     * @param supplierParkingResource
     * @return
     */
    @RequestMapping(value = "/{supplierId}/parking-resource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onParkingCreate(SupplierParkingResource supplierParkingResource) {
        
        HttpResult httpResult = new HttpResult();
        
        supplierParkingResourceService.create(supplierParkingResource);
        
        httpResult.success().message("创建成功");

        return httpResult;
    }

    /**
     *  edit 停车场资源信息
     * 
     * @param supplierId
     * @param parkingId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/parking-resource/{parkingId}/edit", method = RequestMethod.GET)
    public ModelAndView parkingEdit(@PathVariable("supplierId") Long supplierId , @PathVariable("parkingId") Long parkingId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/parking-resource-edit");
        
        SupplierParkingResource parkingResource = supplierParkingResourceService.load(SupplierParkingResource.class, parkingId);
        
        modelAndView.addObject("parkingResource", parkingResource);
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
    }

    /**
     *  on edit 停车场资源信息
     * 
     * @param supplierParkingResource
     * @return
     */
    @RequestMapping(value = "/{supplierId}/parking-resource/{parkingId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onParkingEdit(@Preloading("id") SupplierParkingResource supplierParkingResource) {

        HttpResult httpResult = new HttpResult();
        
        supplierParkingResourceService.edit(supplierParkingResource);

        httpResult.success().message("修改成功");

        return httpResult;
    }

    /**
     * delete 停车场资源信息
     * 
     * @param parkingId
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/parking-resource/{parkingId}/delete", method = RequestMethod.GET)
    public ModelAndView parkingDelete(@PathVariable("parkingId") Long parkingId , @PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/parking-resource-delete");
        
        modelAndView.addObject("parkingId", parkingId);

        return modelAndView;
    }

    /**
     * on delete 停车场资源信息
     * 
     * @param parkingId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/parking-resource/{parkingId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onParkingDelete(@PathVariable("parkingId") Long parkingId) {

        HttpResult httpResult = new HttpResult();
        
        SupplierParkingResource supplierParkingResource = supplierParkingResourceService.load(SupplierParkingResource.class, parkingId);
        
        supplierParkingResourceService.delete(supplierParkingResource);

        httpResult.success().message("删除成功");

        return httpResult;
    }
    
    /**
     * 关键岗位创建panel
     * 
     * @param supplierId
     * @param keyPost
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-staff/create", method = RequestMethod.GET)
    public ModelAndView supplierStaffCreate(@PathVariable("supplierId") Long supplierId ,@RequestParam("keyPost") String keyPost) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-staff-create");
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        modelAndView.addObject("supplier", supplier);
        
        modelAndView.addObject("keyPost",KeyPost.codeOf(keyPost));

        return modelAndView;
        
    }

    /**
     * 关键岗位创建
     * 
     * @param operator
     * @param supplierId
     * @param supplierStaff
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-staff", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSupplierStaffCreate(@Secure(property = "id") Long operator,
                                                          @PathVariable("supplierId") Long supplierId, 
                                                          SupplierStaff supplierStaff) {

        HttpResult httpResult = new HttpResult();
        
        try {

            Supplier supplier = supplierService.load(Supplier.class, supplierId);

            supplierStaffService.create(supplierStaff, supplier, operator);
            
            httpResult.success().message("创建成功");
            
        } catch (Exception e) {
            logger.error("#onSupplierStaffCreate() - create failed, exception={}", e);
            httpResult.fail("500").message("创建失败");
        }

        return httpResult;
        
    }
    
    /**
     * 关键岗位编辑panel
     * 
     * @param supplierId
     * @param staffId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-staff/{staffId}/edit", method = RequestMethod.GET)
    @MenuSetting("submenu-company-info-management")
    public ModelAndView supplierStaffEdit(@PathVariable("supplierId") Long supplierId ,@PathVariable("staffId") Long staffId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-staff-edit");
        
        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        modelAndView.addObject("supplier", supplier);
        
        SupplierStaff supplierStaff = supplierStaffService.load(SupplierStaff.class, staffId);
        modelAndView.addObject("supplierStaff",supplierStaff);

        return modelAndView;
        
    }

    /**
     * 关键岗位编辑
     * 
     * @param supplierStaff
     * @return
     */
    @RequestMapping(value = "/{supplierId}/supplier-staff/{staffId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSupplierStaffEdit(@Preloading("id") SupplierStaff supplierStaff,
                                                        @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        try {
            
            supplierStaffService.edit(supplierStaff, operator);

            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onSupplierStaffEdit() - edit failed, exception={}", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
        
    }
    
    /**
     * create 牵引车运力资源
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat/create", method = RequestMethod.GET, params="vehicle")
    public ModelAndView createTransVehicleStatTractor(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-vehicle-stat-create-vehicle");
        
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
        
    }

    /**
     * on create 牵引车运力资源
     * 
     * @param supplierId
     * @param amounts
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateTransVehicleStatTractor(@PathVariable("supplierId") Long supplierId, @RequestParam("amount") List<Long> amounts) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            transSupplierVehicleStatService.create(supplierId ,amounts);
            
            httpResult.success().message("创建成功");
            
        } catch (Exception e) {
            logger.error("#onCreateTransVehicleStatTractor() - create fail, exception = [{}]", e);
            httpResult.fail("500").message("创建失败");
        }

        return httpResult;
        
    }

    
    /**
     * edit 牵引车运力资源
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat/edit", method = RequestMethod.GET ,params="vehicle")
    public ModelAndView editTransVehicleStat(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-vehicle-stat-edit-vehicle");
        
        List<TransSupplierVehicleStat> transSupplierVehicleStats = transSupplierVehicleStatService.listTransSupplierVehicleStat(supplierId,VehicleType.VEHICLE_TYPE_ZERO,null);
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("transSupplierVehicleStats", transSupplierVehicleStats);
        
        return modelAndView;
    }
    
    /**
     * on edit 牵引车运力资源
     * 
     * @param transSupplierVehicleStatIds
     * @param amounts
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditTransVehicleStat(@RequestParam("id") List<Long>transSupplierVehicleStatIds , 
                                                           @RequestParam("amount") List<Long> amounts) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            transSupplierVehicleStatService.edit(transSupplierVehicleStatIds,amounts);
            
            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onEditTransVehicleStat() - edit fail, exception = [{}]", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
       
    }
    
    /**
     * create 新建挂车运力资源
     * 
     * @param supplierId
     * @param trailerType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat/create", method = RequestMethod.GET ,params="trailer")
    public ModelAndView createTransVehicleStatTrailer(@PathVariable("supplierId") Long supplierId ,@RequestParam("trailerType") String trailerType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-vehicle-stat-create-trailer");
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("trailerType", TrailerType.aliasOf(trailerType));

        return modelAndView;
        
    }

    /**
     * on create 挂车运力资源
     * 
     * @param supplierId
     * @param login
     * @param stat
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat", method = RequestMethod.POST, params="trailer", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateTransVehicleStatTrailer(@PathVariable("supplierId") Long supplierId,
                                                                    @Secure Login login,
                                                                    TransSupplierVehicleStat stat) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            transSupplierVehicleStatService.createTrailer(supplierId, stat, login);
            
            httpResult.success().message("创建成功");
            
        } catch (Exception e) {
            logger.error("#onCreateTransVehicleStatTrailer() - create fail, exception is [{}]", e);
            httpResult.fail("500").message("创建失败");
        }

        return httpResult;
        
    }

    
    /**
     * edit 挂车运力资源
     * 
     * @param supplierId
     * @param trailerType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat/edit", method = RequestMethod.GET, params="trailer")
    public ModelAndView editTransVehicleStatTrailer(@PathVariable("supplierId") Long supplierId, 
                                                    @RequestParam("trailerType") String trailerType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-vehicle-stat-edit-trailer");
        
        TransSupplierVehicleStat stat = transSupplierVehicleStatService.getTransSupplierVehicleStat(supplierId, VehicleType.VEHICLE_TYPE_ONE, TrailerType.aliasOf(trailerType));
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("stat", stat);
        modelAndView.addObject("trailerType", TrailerType.aliasOf(trailerType));
        
        return modelAndView;
        
    }
    
    /**
     * on edit 运力资源
     * 
     * @param login
     * @param stat
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat", method = RequestMethod.PATCH, params="trailer", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditTransVehicleStatTrailer(@Secure Login login,
                                                                  @Preloading(name = "id") TransSupplierVehicleStat stat) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            transSupplierVehicleStatService.edit(stat, login);
            
            httpResult.success().message("挂车类型及满载量信息修改成功");
            
        } catch (Exception e) {
            logger.error("#onEditTransVehicleStatTrailer() - edit trailer fail, exception = [{}]", e);
            httpResult.fail("500").message("挂车类型及满载量信息修改失败");
        }

        return httpResult;
       
    }
    
    
    /**
     * create 其他运力资源 
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat/create", method = RequestMethod.GET ,params="other")
    public ModelAndView createTransVehicleStatOther(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-vehicle-stat-create-other");
        
        modelAndView.addObject("supplierId", supplierId);
        
        return modelAndView;
        
    }

    /**
     * on create 其他运力资源 
     * 
     * @param supplierId
     * @param trailerTypes
     * @param amounts
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat", method = RequestMethod.POST, params="other",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateTransVehicleStatOther(@PathVariable("supplierId") long supplierId,
                                                                  @RequestParam("trailerType") List<TrailerType> trailerTypes,
                                                                  @RequestParam("amount") List<Long> amounts) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            transSupplierVehicleStatService.create(supplierId ,trailerTypes, amounts);
            
            httpResult.success().message("创建成功");
            
        } catch (Exception e) {
            logger.error("#onCreateTransVehicleStatOther() - create fail, exception = [{}]", e);
            httpResult.fail("500").message("创建失败");
        }

        return httpResult;
    }

    
    /**
     * edit 其他运力资源 
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat/edit", method = RequestMethod.GET ,params="other")
    public ModelAndView editTransVehicleStatOther(@PathVariable("supplierId") Long supplierId ) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-vehicle-stat-edit-other");
        
        List<TransSupplierVehicleStat> transSupplierVehicleStats = transSupplierVehicleStatService.listTransSupplierVehicleStat(supplierId,VehicleType.VEHICLE_TYPE_ONE,null);
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("transSupplierVehicleStats", transSupplierVehicleStats);
        
        return modelAndView;
        
    }
    
    /**
     * on edit 其他运力资源 
     * 
     * @param transSupplierVehicleStatIds
     * @param amounts
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-vehicle-stat",  method = RequestMethod.PATCH , params="other" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditTransVehicleStatOther(@RequestParam("id") List<Long>transSupplierVehicleStatIds , 
                                                                @RequestParam("amount") List<Long> amounts) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            transSupplierVehicleStatService.edit(transSupplierVehicleStatIds,amounts);
            
            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onEditTransVehicleStatOther() - edit fail, exception = [{}]", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
       
    }
    
    
    /**
     * edit 运输共方特有字段
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-supplier/edit", method = RequestMethod.GET ,params="panel")
    public ModelAndView editTransSupplier(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-supplier-edit");
        
        TransSupplier transSupplier = transSupplierService.getTransSupplier(supplierId);
        
        modelAndView.addObject("transSupplier", transSupplier);
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
    }
    
    /**
     * on edit 运输共方特有字段
     * 
     * @param transSupplier
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-supplier",  method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditTransSupplier(@Preloading("id") TransSupplier transSupplier) {
        
        HttpResult httpResult = new HttpResult();
        
        transSupplierService.edit(transSupplier);
        
        httpResult.success().message("修改成功");

        return httpResult;
       
    }
    
    /**
     * 运输供方资源信息 - 文件上传
     * 
     * @param supplierId
     * @param type
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/trans-supplier/upload", method = RequestMethod.GET)
    public ModelAndView transSupplierUplaod(@PathVariable("supplierId") Long supplierId,
                                            @RequestParam("type")String type,
                                            @RequestParam("supplierMenuType") String supplierMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/trans-supplier-upload");
        
        modelAndView.addObject("supplierId", supplierId);
        
        TransSupplier transSupplier = transSupplierService.getTransSupplier(supplierId);
        modelAndView.addObject("transSupplier", transSupplier);
        
        modelAndView.addObject("type", type);
        modelAndView.addObject("supplierMenuType", supplierMenuType);
        
        return modelAndView;
        
    }
    
    /**
     * edit 仓储共方特有字段
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier/edit", method = RequestMethod.GET ,params="panel")
    public ModelAndView editWareSupplier(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/ware-supplier-edit");
        
        WareSupplier wareSupplier = wareSupplierService.getWareSupplier(supplierId);
        
        modelAndView.addObject("wareSupplier", wareSupplier);
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
    }
    
    /**
     * on edit 运输共方特有字段
     * 
     * @param login
     * @param wareSupplier
     * @param operationPlatform
     * @param operationDock
     * @param operationWarehouse
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier",  method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditWareSupplier(@Secure Login login,
                                                       @Preloading("id") WareSupplier wareSupplier,
                                                       @RequestParam(name = "operationPlatform", required = false) List<String> operationPlatform,
                                                       @RequestParam(name = "operationDock", required = false) List<String> operationDock,
                                                       @RequestParam(name = "operationWarehouse", required = false) List<String> operationWarehouse) {
        
        HttpResult httpResult = new HttpResult();
        
        wareSupplierService.edit(wareSupplier.edit(login.getId(), operationPlatform, operationDock, operationWarehouse));
        
        httpResult.success().message("修改成功");

        return httpResult;
       
    }
    
    /**
     * 仓储供方资源信息 - 文件上传
     * 
     * @param supplierId
     * @param type
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier/upload", method = RequestMethod.GET)
    public ModelAndView wareSupplierUplaod(@PathVariable("supplierId") Long supplierId,
                                            @RequestParam("type")String type,
                                            @RequestParam("supplierMenuType") String supplierMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/ware-supplier-upload");
        
        modelAndView.addObject("supplierId", supplierId);
        
        WareSupplier wareSupplier = wareSupplierService.getWareSupplier(supplierId);
        modelAndView.addObject("wareSupplier", wareSupplier);
        
        modelAndView.addObject("type", type);
        modelAndView.addObject("supplierMenuType", supplierMenuType);
        
        return modelAndView;
        
    }
    
    /**
     * create 仓储供方
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier-employee/create", method = RequestMethod.GET ,params="panel")
    public ModelAndView createWareSupplierEmployee(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/ware-supplier-employee-create");
        
        modelAndView.addObject("supplierId", supplierId);

        return modelAndView;
    }

    /**
     * on create 仓储供方
     * 
     * @param supplierId
     * @param values
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier-employee", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateWareSupplierEmployee(@PathVariable("supplierId") long supplierId,
                                                                 @RequestParam("value") List<Long> values) {
        
        HttpResult httpResult = new HttpResult();
        
        wareSupplierEmployeeService.create(supplierId , values);
        
        httpResult.success().message("创建成功");

        return httpResult;
        
    }

    
    /**
     * edit 仓储供方
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier-employee/edit", method = RequestMethod.GET ,params="panel")
    public ModelAndView editWareSupplierEmployee(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/ware-supplier-employee-edit");
        
        List<WareSupplierEmployee> wareSupplierEmployees = wareSupplierEmployeeService.listWareSupplierEmployee(supplierId);
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("wareSupplierEmployees", wareSupplierEmployees);
        
        return modelAndView;
        
    }
    
    /**
     * on edit 仓储供方
     * 
     * @param wareSupplierEmployeeIds
     * @param values
     * @return
     */
    @RequestMapping(value = "/{supplierId}/ware-supplier-employee",  method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditWareSupplierEmployee(@RequestParam("id") List<Long>wareSupplierEmployeeIds , 
                                                                  @RequestParam("value") List<Long> values) {
        
        HttpResult httpResult = new HttpResult();
        
        wareSupplierEmployeeService.edit(wareSupplierEmployeeIds,values);
        
        httpResult.success().message("修改成功");

        return httpResult;
       
    }
    
    /**
     * 申请人上传文件信息 - 文件上传
     * 
     * @param supplierId
     * @param recommendationId
     * @param type
     * @param supplierMenuType
     * @return
     */
    @RequestMapping(value = "/{supplierId}/recommendation/{recommendationId}/upload", method = RequestMethod.GET)
    public ModelAndView recommendationUpload(@PathVariable("supplierId") Long supplierId,
                                            @PathVariable("recommendationId") Long recommendationId,
                                            @RequestParam("type")String type,
                                            @RequestParam("supplierMenuType") String supplierMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-recommendation-upload");
        
        modelAndView.addObject("supplierId", supplierId);
        modelAndView.addObject("recommendationId", recommendationId);
        modelAndView.addObject("type", type);
        modelAndView.addObject("supplierMenuType", supplierMenuType);
        
        return modelAndView;
        
    }

    /**
     * 修改付款信息panel
     *
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/edit-payment-info", method = RequestMethod.GET)
    public ModelAndView editPaymentInfo(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/profile/edit/supplier-payment-info-edit");

        SupplierInfo supplierInfo = supplierInfoService.load(supplierId);

        modelAndView.addObject("supplierInfo", supplierInfo);

        return modelAndView;

    }

    /**
     * 修改公司业务信息
     *
     * @param supplierId
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{supplierId}/edit-payment-info", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditPaymentInfo(@PathVariable(name = "supplierId") Long supplierId,
                                                      @Secure(property = "id") Long operator,
                                                      SupplierInfo supplierInfo) {

        HttpResult httpResult = new HttpResult();

        try {

            supplierInfoService.updatePaymentInfo(supplierId, operator, supplierInfo);

            httpResult.success().message("修改成功");

        } catch (Exception e) {
            logger.error("#onSupplierBusinessEdit() - edit fail, exception = [{}]", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;

    }
}